package org.example.struct.array2;

import com.alibaba.fastjson.JSON;

import java.util.Arrays;

/**
 * @author liyishan
 * @date 2024/9/10 09:23
 * @apiNote
 */

public class Test02090GetAverages {
    public static void main(String[] args) {
        int[] nums = {7, 4, 3, 9, 1, 8, 5, 2, 6};
        int k = 3;
        int[] res = getAverages(nums, k);
        System.out.println(JSON.toJSON(res));
    }

    public static int[] getAverages(int[] nums, int k) {
        int n = nums.length;
        int[] avgs = new int[n];
        Arrays.fill(avgs, -1);
        int window = 2 * k + 1;
        if (window > n) {
            return avgs;
        }
        long sum = 0;
        for (int i = 0; i < window; i++) {
            sum += nums[i];
        }
        avgs[k] = (int) (sum / window);
        for (int i = k + 1; i < n-k; i++) {
            sum -= nums[i - k - 1];
            sum += nums[i + k];
            avgs[i] = (int) (sum / window);
        }
        return avgs;
    }


}
